package org.jeecg.modules.basic.entry.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import net.sf.json.JSONObject;
import org.jeecg.common.api.vo.Result;


import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.basic.entry.entity.LwgyInoutRecord;
import org.jeecg.modules.basic.entry.service.ILwgyInoutRecordService;
import org.jeecg.modules.basic.entry.util.SenseTimeToDevice;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import static org.jeecg.modules.basic.entry.util.HttpRequestToPrivate.*;


/**
 * 商汤对接类
 *
 * */
@Log
@Api(tags = "SenseTime")
@RestController
@RequestMapping("/entryRest")

public class InoutConnectControllerToPrivate {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ILwgyInoutRecordService iLwgyInoutRecordService;
    //济宁学院大屏接口地址
    String bigUrl="http://172.20.0.250:9090/getServerInfo";
    //String bigUrl="http://university.vip3gz.91tunnel.com/getServerInfo";



    /**
     * 从商汤获取人脸设备的进出记录
     * */
    @AutoLog(value = "SenseTime-人脸识别闸机对接")
    @ApiOperation(value = "SenseTime人脸识别闸机", notes = "SenseTime人脸识别闸机")
    @PostMapping("/getOutAndIn")
    @ResponseBody
    public Result<?> getOutAndIn(@RequestBody Map<String, Object> msg) {
        //SenseTime商汤数据推送开始=========
        try {
            log.info("收到一条进出消息："+msg);
         /*   //掉大屏接口，将数据传入大屏
            try{
                sendPost(bigUrl, msg,null,null);
            }catch (Exception e){
                System.out.println("---传入大屏数据保存："+e);
            }*/

            JSONObject json = JSONObject.fromObject(msg);

            //掉大屏接口，将数据传入大屏
            try{
                log.info("开始进入接口：");
                String result = sendPostByJSON(bigUrl, json.toString());
                log.info("刘哥说没走这个接口，娜姐让我加个打印看看：");
            }catch (Exception e){
                log.info("---传入大屏数据保存："+e);
            }finally {
                log.info("---json："+json);
                JSONObject data = json.getJSONObject("data");
                int data_userId = data.getInt("userId");//被识别人id
                String data_name = data.getString("userName");//被识别人姓名
                int data_type = data.getInt("type");//人员身份类型
                String data_direction = SenseTimeToDevice.getDirection(data.getString("ldid"));//进出方向
                String data_signDate = data.getString("signTimeStr");//识别时间（秒级） 1617944455
                String data_signAvatar = data.getString("signAvatar");//识别头像
                String data_jobNumber = data.getString("jobNumber");//被识别人工号
                int data_entryMode = data.getInt("entryMode");//核验模式（1、刷脸；2、二维码；3、刷卡；4：刷脸+刷卡；5：刷身份证；6：刷脸且刷身份证）
                int data_abnormalType = data.getInt("abnormalType");//识别记录异常类型；0-无;10001-人证不匹配;10002-人卡不匹配;10003-人码不匹配;20001-访客不在有效期内;20002-不在通行时间内；30001-无效身份证;30002-无效IC卡;30003-无效二维码;40001-体温异常;50001-未戴口罩
                String data_location = SenseTimeToDevice.getLocation(data.getString("ldid"));//设备位置
                //时间字符串转成时间格式


                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = sdf.parse(data_signDate);





                LwgyInoutRecord lwgyInoutRecor = new LwgyInoutRecord();
                String id = UUIDGenerator.generate();
                lwgyInoutRecor.setId(id);
                //获取被识别人的工号data_jobNumber获取相关的studentId
            /*LwgyBaseStudent stu =iLwgyInoutRecordService.queryStuByNumString(data_jobNumber);
            System.out.println("=======获取的相关学生的id为"+stu.getId());*/

                //lwgyInoutRecor.setUserId(stu.getId()==null?data_jobNumber:stu.getId());//userId修改为学生id
                lwgyInoutRecor.setGuestId(String.valueOf(data_userId));//第三方id
                lwgyInoutRecor.setUserId(data_jobNumber);//学生或老师Id
                lwgyInoutRecor.setUserName(String.valueOf(data_name));
                lwgyInoutRecor.setDirection(String.valueOf(data_direction));
                lwgyInoutRecor.setType(String.valueOf(data_type));

                lwgyInoutRecor.setRecordDate(date);
                lwgyInoutRecor.setHeadImg(String.valueOf(data_signAvatar));
                lwgyInoutRecor.setMode(String.valueOf(data_entryMode));

                lwgyInoutRecor.setLocation(String.valueOf(data_location));

                iLwgyInoutRecordService.save(lwgyInoutRecor);
                //iLwgyInoutRecordService.powerController(lwgyInoutRecor);调领导接口，因速度太慢，暂时放弃该方案
                iLwgyInoutRecordService.powerInApartment(lwgyInoutRecor);//保存到表apartment,领导用后台程序开关电

                //     lwgyInoutRecor.setAbnormalType(String.valueOf(data_abnormalType));
                //
                ////转存到消息中间件
                //CorrelationData correlationData = new CorrelationData();
                //correlationData.setId(lwgyInoutRecor.getId());
                //rabbitTemplate.convertAndSend("order-exchange",
                //"order.abcd",
                //lwgyInoutRecor,
                //correlationData);
            }


        } catch (Exception e) {
            return Result.error(1, "false");
        }
        return Result.OK("success");

    }

}

